<?php
include_once("../include/init.php");
	if(!isset($_SESSION['user'])){
		msg("请先登录","index.php");
		exit;
	}
	if(isset($_GET['id'])){
		//获取当前修改的商品的ID
		$id = $_GET['id'];
		//获取当前修改商品的类型
		$type = $_GET['type'];
		if($type == 3){
			//当类型是3 的时候就是批量修改我们需要把文本框的数字获取到
			$num = (int)$_GET['num'];

		}
		//在修改之前需要得到当前的修改的商品的库存信息
$sql = "select * from tk_product where pid =$id";
$res = mysql_query($sql);
$info = [];
if(mysql_num_rows($res)>0){
	while($list = mysql_fetch_assoc($res)){
		$info[] =$list;
	}
}	
		foreach($info as $vo){
			if($id == $vo['pid']){
				//从商品信息数组中找到要修改的那件商品
				$arr = $vo;
			}
		}
		//根据不同的类型开始修改
		foreach($_SESSION['goods'] as &$vo){
			if($id == $vo['pid']){
				if($type == 1){
					$vo['num']-=1;
				}
				if($type == 2){
					$vo['num']+=1;
				}
				if($type == 3){
					$vo['num'] = $num;
				}
				if($vo['num'] > $arr['snums']){
					$vo['num'] = $arr['snums'];
				}
			}
		}
		header("location:cart.php");
	}
?>